﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using DBControl;
using System.Configuration;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;
using Top.Api.Domain;

namespace ProjectX
{
    public partial class buy : Errorbase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["userNick"] != null)
                {
                    if (userBuy() < 5)
                    {
                        Response.Redirect("/MsgWeb.html",false);
                    }
                    else
                    {
                        Bind_list();
                        //this.cit1.Attributes.Add("onchange", "Bindlist(this,2)");
                    }
                    this.username.Text = Session["userNick"].ToString();
                    this.loginlink.Visible = false;
                    this.reglink.Visible = false;
                    this.logout.Visible = true;
                }
                else
                {
                    Response.Redirect("/Login.html",false);
                }
                
            }
        }
        /// <summary>
        /// 判断当前用户积分是否足够发起购买
        /// </summary>
        /// <returns></returns>
        private int userBuy()
        {
            string buyname = Session["userNick"].ToString();
            string updatesql = "SELECT userpoint FROM UserPoint WHERE userName = '" + buyname + "'";
            SqlDbHelper sdh = new SqlDbHelper();
            int result = int.Parse(sdh.ExecuteScalar(updatesql).ToString());
            return result;
        }

        private void Bind_list()
        {
            ITopClient itop = new DefaultTopClient(SystemLib.appurl, SystemLib.appkey, SystemLib.appsecret);

            ItemcatsGetRequest itemsget = new ItemcatsGetRequest();

            itemsget.Fields = "cid,parent_cid,name,is_parent,status,sort_order";
            itemsget.ParentCid = 0;
            
            ItemcatsGetResponse itemcatsresponse = itop.Execute(itemsget);

            List<ItemCat> itemcats = itemcatsresponse.ItemCats.FindAll(delegate(ItemCat p) { return p.Status == "normal"; });
            itemcats.Sort(delegate(ItemCat p1, ItemCat p2) { return p1.SortOrder.CompareTo(p2.SortOrder); });

            foreach (var item in itemcats)
            {
                ListItem li;
                if (item.IsParent)
                {
                    li = new ListItem(item.Name, item.Cid.ToString());
                }
                else
                {
                    li = new ListItem(item.Name, item.Cid.ToString());
                }
                this.cit1.Items.Add(li);
            }
        }

        protected void logout_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Write("<script language='javascript'>window.parent.location.href='/'</script>");
        }
           
    }
}